<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>用户绑定列表</title>
</head>
<link rel="stylesheet" href="https://cdn.bootcss.com/weui/1.1.3/style/weui.min.css">
<link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.1/css/jquery-weui.min.css">

<link rel="stylesheet" href="../../src/dist/css/page.css">

<body>
<div class="weui-cells user-bindingList">
    <div class="weui-cells__tips">绑定用户列表</div>
    <div class="weui-cells" id="listDemo">
        <img src="../../src/dist/img/nodata.png" alt="" class="nodataImg">
    </div>
    <div class="button_sp_area">
        <a href="javascript:;" class="weui-btn weui-btn_primary weui-btn_disabled" onclick="bindingNewUser()">绑定新用户</a>
    </div>
</div>

<script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/jquery-weui/1.2.1/js/jquery-weui.min.js"></script>

<!-- Dist -->
<script src="../../src/dist/js/index.js"></script>
<script src="../../src/dist/js/utils.js"></script>
<script src="../../src/dist/js/request.js"></script>
<script>
    /*
    * 用户绑定基本流程：
    * 1.先请求接口获得一个URL
    * 2.跳转请求的URL，腾讯会重定向到本地的页面
    * 3.解析重定向的页面，获取code
    * 4.传递code，获取到openid
    * 5.openid是微信的唯一标识，拿到之后就可以请求自己的后台了（获取绑定用户列表、解绑、绑定等）
    * */
    var userData = [],
      wcode = '',
      openid = '';
    $(function(){
        var paras = location.search;            //search获得地址中的参数，内容为'?itemId=12'
        var result = paras.match(/[^\?&]*=[^&]*/g);     //match是字符串中符合的字段一个一个取出来，result中的值为['login=xx','table=admin']
        paras = {};                    //让paras变成没有内容的json对象
        for (i in result) {
            var temp = result[i].split('=');    //split()将一个字符串分解成一个数组,两次遍历result中的值分别为['itemId','xx']
            paras[temp[0]] = temp[1];
        }
		let code = paras.code;     //根据参数名"itemId"，获取参数值

        console.log(code)

		getUserListPHP()
        return
      if(code){
        wcode = code
        getOpenId()
      }else{
        getWeiXinURL()
      }
    })
    // 获取openid
    function getOpenId() {
        get("/wechat/binding/toIdentityBinding?code="+wcode, {}, function (data) {
            if (data.code === 0) {
				openid = data.data
                $(".weui-btn_disabled").removeClass('weui-btn_disabled')
              // 获取绑定用户列表
				getUserList()

            } else {
                $.toast('获取openid失败!', 'forbidden');
            }
        }, false, true);
    }

    //获取URL
    function getWeiXinURL() {
        get("/wechat/binding/getAuthUrl", {}, function (data) {
            if (data.code === 0) {
                let url = data.data
                url='https://open.weixin.qq.com/connect/oauth2/authorize?redirect_uri=http://zbcsrq.com:33813/pages/user/bangdingList.html&appid=wx60c25e54627b700d&response_type=code&scope=snsapi_base&state=#wechat_redirect'
                location.replace(url)
            } else {
                $.toast('获取URL失败!', 'forbidden');
            }
        }, false, true);
    }
    // 绑定新用户
    function bindingNewUser() {
        if(openid === ''){
            $.toast('未获取到openid，请重新进入页面!', 'forbidden');
            return
        }
        location.href  = './bangding.html?openid='+ openid
	}

	// 获取绑定用户列表
    function getUserListPHP() {
        $.ajax({
            type: "POST",
            url: "../../src/php/requestOnline.php",
            data: {
                act: 'CSRQ_getUserList'
            },
            success: function (data) {
                data = eval('(' + data + ')')
                userData = data
                createListDemo()
            },
            error: function (data) {

            }
        });
	}
    function getUserList(){
        get("/wechat/binding/getList?openid="+ openid, {}, function (data) {
            if (data.code === 0) {
				userData = data.data
				createListDemo()
            } else {
                $.toast('获取绑定用户失败!', 'forbidden');
            }
        }, false, true);
    }
    // 创建列表
    function createListDemo() {
      let d = userData
      let h = ''
      for(let [index, item] of d.entries()){
      	h+='<div class="weui-cell" onclick="deleteUser('+ index +')">\n' +
            '            <div class="weui-cell__hd">\n' +
            '                <i class="iconfont iconzhanghao"></i>\n' +
            '            </div>\n' +
            '            <div class="weui-cell__bd">\n' +
            '                <p>'+item['name']+'</p>\n' +
            '            </div>\n' +
            '            <div class="weui-cell__ft">'+item['phonenumber']+'</div>\n' +
            '        </div>'
      }
      $("#listDemo").html(h)
	}
	// 解除绑定
    function deleteUser(i) {
        $.actions({
            actions: [{
                text: "解除绑定",
                onClick: function () {
                    //do something
                    let customid = userData[i]['id']
                    get("/wechat/binding/delBinding?customid="+ customid + "&openid="+ openid, {}, function (data) {
                        if (data.code === 0) {
                            location = location
                        } else {
                            $.toast('解除绑定失败!', 'forbidden');
                        }
                    }, false, true);
                }
            }, {
                text: "查看缴费记录",
                onClick: function () {
                    //do something
                    let customid = userData[i]['id']
					    location.href = './paymentRecords.html?openid=' + openid + '&customid='+ customid
                }
            }, {
                text: "查看安检记录",
                onClick: function () {
                    //do something
                    let customid = userData[i]['id']
                    location.href = '../SecurityRecord/list.html?openid=' + openid + '&customid='+ customid
                }
            }, {
                text: "查看预约记录",
                onClick: function () {
                    //do something
                    let customid = userData[i]['id']
                    location.href = '../Appointment/list.html?openid=' + openid + '&customid='+ customid
                }
            }]
        });
	}
</script>
</body>
</html>
